package win;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class JavaWindowWithImageAndButtons extends JFrame {
    public JavaWindowWithImageAndButtons() {
        // 设置窗口标题
        setTitle("我的Java窗口");

        // 获取屏幕大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;

        // 设置窗口大小为屏幕大小的三分之二
        setSize(screenWidth * 2 / 3, screenHeight * 2 / 3);

        // 设置窗口图标
        try {
//            URL iconURL = getClass().getResource("src/main/java/win/a.ico");
            URL iconURL = getClass().getResource("a.ico");
            ImageIcon icon = new ImageIcon(iconURL);
            setIconImage(icon.getImage());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 设置窗口关闭时的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建面板
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        // 加载图片
        ImageIcon imageIcon = new ImageIcon("src/main/java/win/bg_zhuye.jp");
        JLabel imageLabel = new JLabel(imageIcon);
        panel.add(imageLabel, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());

        // 创建四个按钮
        JButton button1 = new JButton("按钮1");
        JButton button2 = new JButton("按钮2");
        JButton button3 = new JButton("按钮3");
        JButton button4 = new JButton("按钮4");

        // 为按钮添加动作监听器（这里只是简单地弹出一个对话框）
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "你点击了按钮1");
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "你点击了按钮2");
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "你点击了按钮3");
            }
        });
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "你点击了按钮4");
            }
        });

        // 将按钮添加到按钮面板
        buttonPanel.add(button1);
        buttonPanel.add(button2);
        buttonPanel.add(button3);
        buttonPanel.add(button4);

        // 将按钮面板添加到主面板
        panel.add(buttonPanel, BorderLayout.SOUTH);

        // 将主面板添加到窗口
        add(panel);

        // 设置窗口位置为屏幕中央
        setLocation(screenWidth / 6, screenHeight / 6);

        // 显示窗口
        setVisible(true);
    }

    public static void main(String[] args) {
        new JavaWindowWithImageAndButtons();
    }
}